/**
 *
 */
package jp.seraph.same.controller;

import jp.seraph.jsade.model.AngleVelocityCalculator;
import jp.seraph.jsade.model.JointIdentifier;
import jp.seraph.jsade.model.JointIdentifierContainer;
import jp.seraph.jspf.pose.PoseManager;
import jp.seraph.same.message.MessageUtil;
import jp.seraph.same.model.DefaultMotionElementModel;
import jp.seraph.same.model.MotionElementModel;
import jp.seraph.same.model.MotionSessionModel;

/**
 *
 */
public class DefaultMotionSessionController implements MotionSessionController {
    public DefaultMotionSessionController(MotionSessionModel aModel, MotionController aParentController, PoseManager aPoseManager, JointIdentifierContainer aIdContainer, AngleVelocityCalculator aDefaultCalculator) {
        mModel = aModel;
        mPoseManager = aPoseManager;
        mDefaultCalculator = aDefaultCalculator;
        mParentController = aParentController;
        mIdContainer = aIdContainer;
    }

    private MotionController mParentController;

    private MotionSessionModel mModel;

    private PoseManager mPoseManager;

    private AngleVelocityCalculator mDefaultCalculator;

    private JointIdentifierContainer mIdContainer;

    /**
     * @see jp.seraph.same.controller.MotionSessionController#createElement(int)
     */
    public MotionElementModel createElement(String aElementName) {
        return createElement(aElementName, true);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionSessionController#createElement(int, boolean)
     */
    public MotionElementModel createElement(String aElementName, boolean aSelectAll) {
        DefaultMotionElementModel tNewModel = new DefaultMotionElementModel(aElementName, mPoseManager, mDefaultCalculator);
        mModel.add(tNewModel);

        if (aSelectAll) {
            for (JointIdentifier tId : mIdContainer) {
                tNewModel.selectJoint(tId);
            }
        }

        return tNewModel;
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionSessionController#getElement(int)
     */
    public MotionElementModel getElement(String aElementName) {
        return mModel.get(aElementName);
    }

    /**
     * @see jp.seraph.same.controller.MotionSessionController#removeElement(int)
     */
    public void removeElement(String aElementName) {
        mModel.remove(aElementName);

        // 前提となるElement名から、削除されたをものを探して消す
        for (MotionElementModel tModel : mModel) {
            if(tModel.getConditionElementName().equals(aElementName))
                tModel.setConditionElementName("");
        }
    }

    public void removeElement(MotionElementModel aTarget) {
        this.removeElement(aTarget.getName());
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionSessionController#renameElement(java.lang.String, java.lang.String)
     */
    public void renameElement(String aFrom, String aTo) {
        if("".equals(aTo))
            throw new IllegalArgumentException(MessageUtil.cannotUseEmptyString());

        if(mModel.get(aTo) != null)
            throw new RuntimeException(MessageUtil.duplicateElementName(aTo));

        MotionElementModel tTargetModel = this.getElement(aFrom);
        this.removeElement(aFrom);
        tTargetModel.setName(aTo);
        mModel.add(tTargetModel);

        // 前提となるElement名設定を変更
        for (MotionElementModel tModel : mModel) {
            if(tModel.getConditionElementName().equals(aFrom))
                tModel.setConditionElementName(aTo);
        }
    }

    public void delete() {
        mParentController.removeSession(mModel);
    }

    public void moveDown() {
        mParentController.moveDown(mModel);
    }

    public void moveUp() {
        mParentController.moveUp(mModel);
    }

    public void rename(String aNewName) {
        mParentController.renameSession(mModel.getName(), aNewName);
    }
}
